package com.swl.controller;

import com.swl.util.VerifyCode;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@RestController
public class CodeController {
    @RequestMapping("/code")
    public String getCode(HttpServletRequest request, HttpServletResponse response)throws Exception {
        System.out.println("获取验证码成功");
        //1、保存图片
        VerifyCode verifyCode = new VerifyCode();
        BufferedImage image = verifyCode.getImage();
        //2、保存图片上的文本到session域中
        String text = verifyCode.getText();
        System.out.println("自动生成的验证码为:" + text);
//            model.addAttribute("session_vcode",text);
        HttpSession session = request.getSession();
        session.setAttribute("session_vcode", text);
        // 3、把图片响应给客户端
        VerifyCode.output(image, response.getOutputStream());

//        String img = request.getParameter("img");
//        System.out.println("输入的验证码的值" + img);
        return text;
    }
}
